package main

import (
	"gbox-one-machine/conf"
	"gbox-one-machine/log"
	"gbox-one-machine/models"
	"gbox-one-machine/rpc"
	"github.com/gorilla/websocket"
	"net/url"
	"time"
)

func main() {
	cf := &conf.Config{
		Log: &conf.Log{
			Path: "./var/log",
		},
		App: &conf.App{
			Debug: false,
		},
	}
	log.InitLogger(cf)
	//urlDefine := &url.URL{Scheme: "ws", Host: "localhost:34543", Path: "/api/v1/one_machine/bind"}
	urlDefine := &url.URL{Scheme: "ws", Host: "10.2.72.131:3454", Path: "/api/v1/one_machine/bind"}
	//urlDefine := &url.URL{Scheme: "ws", Host: "127.0.0.1:3454", Path: "/api/v1/one_machine/bind"}
	r, err := rpc.NewJSONRPC(&rpc.JSONRPCOpts{
		URL: urlDefine,
	})
	if err != nil {
		panic(err)
	}

	// for error test
	ws, _, err := websocket.DefaultDialer.Dial(urlDefine.String(), nil)
	if err != nil {
		panic(err)
	}

	// recv ws
	go func() {
		for {
			_, buf, err := ws.ReadMessage()
			if err != nil {
				log.Logger.Errorf("recv row ws err: %s", err)
				return
			}
			log.Logger.Infof("recv row ws resp: %s", buf)
		}
	}()

	// request send
	for {
		<-time.After(time.Second * 1)
		req := &models.BreathingLamp{Frequency: 5}
		//relpy := ""
		relpy := &rpc.Response{}
		err := r.Call(rpc.CallTypeRequest, "breathing-lamp", req, &relpy)
		if err != nil {
			log.Logger.Errorf("call request err: %s", err.Error())
			continue
		}
		log.Logger.Infof("call request: %s", relpy)
	}
}
